/*
 * @Description:
 * @Author:
 * @Date: 2024-10-21 21:54:52
 * @LastEditTime: 2024-10-23 21:42:13
 * @LastEditors: XXX
 */
import log from '../utils/logger'
import { MiddleWare } from 'type'

export type ResultInfo = {
  code: number,
  msg?: string,
  data?: any,
  err?: any
}

/**
 * 执行结果 handler 用来给每个响应对象包装响应码等
 */
export const ResultHander: MiddleWare = () => async (_ctx, next) => {
  const r: ResultInfo = { code: 0 }
  try {
    const data: any = await next()
    r.code = 0
    r.msg = 'success'
    r.data = data
  } catch (error) {
    log.error(error)
    log.error('xxx' + error.statusCode)
    r.code = error.statusCode
    switch (error.statusCode) {
      case 102:
        r.msg = '用户不存在'
        break
      default:
        break
    }
  }

  _ctx.body = r
}
